#import "ProvinceCityPicker.h"
extern int global_bigId;
@implementation ProvinceCityPicker
@synthesize dic;
@synthesize datePickerView;
@synthesize selectLabel,provinceID,cityID,zoneID;
@synthesize class2Array,class1Array,class3Array;

-(void) addLabel {
    @try{
        if (selectLabel == nil) {
            selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 87, 320, 43)];
            selectLabel.text = @"";
            selectLabel.font = [UIFont systemFontOfSize:12];
            selectLabel.textAlignment = UITextAlignmentCenter;
            selectLabel.backgroundColor = [UIColor grayColor];
            [self.view addSubview:selectLabel];
            
            
        }
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
}
- (void)viewDidLoad {
    @try{
        [super viewDidLoad];
        [self addLabel];
        
        self.datePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 460, 320, 460)];
        self.datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.datePickerView.showsSelectionIndicator = YES;
        self.datePickerView.delegate = self;
        self.datePickerView.dataSource = self;
        
        [self.view addSubview:self.datePickerView];
		
        
        dao = [[Dao alloc]init];
        [dao openDB];
        
        self.class1Array = [dao findProvinceList];
        NSLog(@"self.class1Array=%@",self.class1Array);
        
        
        [UIView beginAnimations: @"Animation" context:nil];
        [UIView setAnimationDuration:0.3];
        //datePickerView.frame = CGRectMake(0, 245, 320, 460);
        datePickerView.frame = CGRectMake(0, 0, 320, 480);
        [UIView commitAnimations];
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
	
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	
	NSString *str = @"";
    @try{
        if (component == 0) {
            ProvincePO* po=[self.class1Array objectAtIndex:row];
            
            return po.provinceName;
            
            
            
        }
        if (component == 1) {
            CityPO* po=[self.class2Array objectAtIndex:row];
            return po.cityName;
        }
        if (component == 2) {
            ZonePO* po=[self.class3Array objectAtIndex:row];
            return po.zoneName;
        
        }
        
        
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
	
	return str;
	
	
}




- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	NSInteger integer;
    @try{
        if (component == 0){
            integer=[self.class1Array count];
        }
        if(component==1){
            integer=[self.class2Array count];
        }
        if(component==2){
            integer=[self.class3Array count];
        }
        
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
	
	return integer;
	
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	CGFloat componentWidth = 0.0;
	
	if (component == 0)
	{
		componentWidth =80.0;
	}
	else
	{
		componentWidth =130.0;
	}
	
	
	return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
	return 40.0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	@try{
        NSLog(@"component==%d",component);
        NSString* nclass=@"";
        if (component == 0) {
            
            ProvincePO* po=[self.class1Array objectAtIndex:row];
            
            self.province=po.provinceName;
            
            provinceID=po.provinceID;
            if (provinceID>0) {
                self.class2Array=[dao findCityList:provinceID];
                
                selectLabel.text=nclass;
                [pickerView reloadComponent:1];
                ///////
                CityPO* citypo=[self.class2Array objectAtIndex:0];
                //这里必须使用self
                self.city=citypo.cityName;
                cityID=citypo.cityID;
                
                
                self.class3Array=[dao findZoneList:cityID];
                ZonePO* zonePO=[self.class3Array objectAtIndex:0];
                self.zone=zonePO.zoneName;
                [pickerView reloadComponent:2];
            }
            
            
            
        }
        if (component == 1) {
            NSLog(@"进入联动  %@",class2Array);
            CityPO* po=[class2Array objectAtIndex:row];
            
            self.city=po.cityName;
            cityID=po.cityID;
            
            selectLabel.text=city;
            //NSLog(@"进入空设置....");
            self.class3Array=[dao findZoneList:cityID];
            ZonePO* zonePO=[self.class3Array objectAtIndex:0];
            self.zone=zonePO.zoneName;
            [pickerView reloadComponent:2];
        }
        if (component ==2) {
            ZonePO* po=[self.class3Array objectAtIndex:row] ;
            
            self.zone=po.zoneName;
            zoneID=po.zoneID;
            
            selectLabel.text=nclass;
            //NSLog(@"进入空设置....");
        }

    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
}

//PickerView的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 3;
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    
}


@end


